package com.icss.ah.shopVipSet.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.shopVipSet.bean.ShopVipSetBean;
import com.icss.j2ee.util.UUID;
/**
 * 店铺会员设置Action
 * @author wangjun
 *
 */
@Controller
@Scope("prototype")
public class ShopVipSetAction extends BaseAction{
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	private ShopVipSetBean shopVip = new ShopVipSetBean();
	// json串
	private Json jsonText;
	/**
	 * 进入会员设置主界面
	 * @return
	 */
	public String toShopVip(){
		try {
			request = this.getRequest();
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ShopVipSet|异常：",e);
		}
		return "toShopVip";
	}
	
	
	/**
	 * 查询会员设置列表
	 */
	public void queryVipList(){
		request = this.getRequest();
		String result = "";
		try {
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();   //获取店铺id 
			String corpId=userInfo.getCorpId();   //公众号id 
			
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("corpId",corpId);      //公众号id
			map.put("shopId",shopId);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> vipList = new ArrayList();
			vipList = shopVipSetService.getVipList(map);	//查询列表
			long vipCount = shopVipSetService.getVipCount(map);//查询总记录数
			dataGridJson.setRows(vipList);
			dataGridJson.setTotal(vipCount);
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("AdInfo|查询广告列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("AdInfo|查询广告列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	
	/**
	 * 启用操作
	 * @return
	 */
	public String start(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String id=request.getParameter("id");
			String status=request.getParameter("status");
			HashMap map = new HashMap();
			map.put("id", id);
			map.put("status",status);
			shopVipSetService.updateStatus(map);
			jsonText.setSuccess(true);
			jsonText.setMsg("启用成功！");
			
		} catch (Exception e) {
			jsonText.setMsg("启用失败！");
			jsonText.setSuccess(false);
		}
		return "jsonText";
		
	}
	
	/**
	 * 停用操作
	 * @return
	 */
	public String stop(){
		request = this.getRequest();
		jsonText = new Json();
		try {
			String id=request.getParameter("id");
			String status=request.getParameter("status");
			HashMap map = new HashMap();
			map.put("id", id);
			map.put("status",status);
			shopVipSetService.updateStatus(map);
			jsonText.setSuccess(true);
			jsonText.setMsg("停用成功！");
			
		} catch (Exception e) {
			jsonText.setMsg("停用失败！");
			jsonText.setSuccess(false);
		}
		return "jsonText";
		
	}
	
	/**
	 * 进入保存店铺会员页面
	 * @return
	 */
	public String toEditShopVip(){
		request = this.getRequest();
		String uuid=request.getParameter("uuid"); //店铺会员设置id
		uuid = uuid == null ? "" : uuid;
		String act=request.getParameter("act");  //添加或修改的标识符
		String total=request.getParameter("total");	//店铺会员设置记录数
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();   //获取店铺id 
		String corpId=userInfo.getCorpId();   //公众号id 
		
		if ("add".equals(act)) {
			request.setAttribute("act", "add");
			HashMap map = new HashMap();
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			HashMap thebeforeMap = shopVipSetService.queryTheBefShopVip(map); //新增时查询前一等级的店铺会员信息
			request.setAttribute("thebeforeMap", thebeforeMap);
		} else if ("edit".equals(act)) {
			HashMap map = new HashMap();
			map.put("uuid", uuid);
			map.put("corpId", corpId);
			map.put("shopId", shopId);
			HashMap theVipMap = shopVipSetService.queryTheShopVip(map); //修改时查询当前修改的信息
			String iSortNumo = theVipMap.get("SORT_NUM").toString();
			int iSortNum=iSortNumo==null || iSortNumo.equals("") ? 0 : Integer.parseInt(iSortNumo);
			map.put("iSortNum",iSortNum+1);
			HashMap theNextVipMap = shopVipSetService.querytheNextVip(map); //修改时查询下一级别的信息
			request.setAttribute("theNextVipMap",theNextVipMap);
			request.setAttribute("theVipMap", theVipMap);
			request.setAttribute("act", "edit");
			request.setAttribute("uuid", uuid);
		}
		request.setAttribute("total", total);
		return "toShopVipEdit";
	}
	
	public String saveVip() {
		request = this.getRequest();
		jsonText = new Json();
		try {
			boolean result=false;
			String beforeSort=request.getParameter("beforeSort"); //修改前的排序
			String act=request.getParameter("act"); //添加或修改标示符
			String uuid=request.getParameter("uuid"); //店铺会员设置id
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();   //获取店铺id 
			String corpId=userInfo.getCorpId();   //公众号id 
			
			uuid = uuid == null ? "":uuid;
			shopVip.setShopId(shopId);
			shopVip.setCorpId(corpId);
			result = shopVipSetService.saveVipInfo(shopVip,act,beforeSort,uuid);
			if(result==true){
				jsonText.setSuccess(true);
				jsonText.setMsg("操作成功！");
			}else{
				jsonText.setSuccess(false);
				jsonText.setMsg("操作失败！");
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return "jsonText";
	}



	public Json getJsonText() {
		return jsonText;
	}


	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}


	public ShopVipSetBean getShopVip() {
		return shopVip;
	}


	public void setShopVip(ShopVipSetBean shopVip) {
		this.shopVip = shopVip;
	}
	
}
